package yxy.game.pm2.command.bandit;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.crusadedateduties;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BanditMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.bandit.Bandit;
import yxy.game.pm2.module.bandit.BanditHandle;
import yxy.game.pm2.module.bandit.BanditModule;

/** 剿匪 剿匪任务加速 */
@MessageType(proto = Command.BanditTaskSpeedUp, classtype = BanditMessage.BanditTaskSpeedUpRequest.class)
public class CMD_BanditTaskSpeedUp extends Command<BanditMessage.BanditTaskSpeedUpRequest> {

	@Override
	public void execute(MessageSource messageSource, BanditMessage.BanditTaskSpeedUpRequest request) {
		String playerCode = messageSource.getTag();
		int taskId = request.getId();

		BanditMessage.BanditTaskSpeedUpResponse.Builder response = BanditMessage.BanditTaskSpeedUpResponse.newBuilder();

		BanditModule banditModule = BanditModule.get(playerCode);
		try {
			if (banditModule.tryLock()) {

				Bandit bandit = banditModule.task(taskId);
				if (bandit == null || bandit.getEndTime() == null) {
					send(messageSource, response, Result.BanditTaskSpeedUpFail);
					return;
				}
				crusadedateduties data = DataCenter.getData(bandit.getDataId(), crusadedateduties.class);
				if (bandit.getEndTime().isAfter(DateTime.now())) {
					Currency cost = Currency.create(data.getUpcosttype(), data.getUpcostid(), data.getUpcostnum());
					try {
						new CurrencyHandle().cost(playerCode, CCReason.剿匪, cost);
					} catch (CurrencyNotEnoughException e) {
						send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
						return;
					}
				}

				Currency reward = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
				new CurrencyHandle().addCurrency(playerCode, CCReason.剿匪, reward);

				bandit.setDataId(0);
				bandit.setEndTime(null);
				bandit.setSendingHeroCode(null);
				banditModule.set();

				response.addRewards(new CurrencyHandle().packItem(reward));

				send(messageSource, response, Result.Success);

				new BanditHandle().updateBanditTip(playerCode);
				//todo 每次循环的前半小时内不开启榜
				ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
				DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
				if (!DateTime.now().isBefore(begin)) {
					RankRushHandle.addRankRushData(playerCode, 1, RankRushEnum.冲榜_剿匪次数.getType());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			banditModule.unlock();
		}
	}

	private void send(MessageSource messageSource, BanditMessage.BanditTaskSpeedUpResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}